﻿namespace DimensionsHelper.Client.WinUi.Models;

public partial class StringPairCollection : ObservableCollection<StringPair>
{
    public IRelayCommand ClearCommand => new RelayCommand(Clear);

    [RelayCommand]
    private void AddNew()
    {
        StringPair pair = new()
        {
            Collection = this
        };

        Add(pair);
    }


    public void FromDictionary(Dictionary<string, string> dictionary)
    {
        Clear();

        foreach ((string key, string value) in dictionary)
        {
            Add(new StringPair
            {
                Key = key,
                Value = value,
                Collection = this
            });
        }
    }


    public void ToDictionary(Dictionary<string, string> dictionary)
    {
        dictionary.Clear();

        foreach (StringPair pair in this)
        {
            dictionary.TryAdd(pair.Key, pair.Value);
        }
    }
}